import  time,os
reports_path = os.path.abspath(os.path.dirname(os.getcwd()))+"\\reports"

def rename(path,environment):
    try:
        '该文件夹下所有的文件（包括文件夹）'
        FileList = os.listdir(path)
        '遍历所有文件'
        for files in FileList:
            '原来的文件路径'
            oldDirPath = os.path.join(path, files)
            '如果是文件夹则递归调用'
            if os.path.isdir(oldDirPath):
                # rename(oldDirPath)
                continue
            '文件名'
            fileName = os.path.splitext(files)[0]
            '文件扩展名'
            fileType = os.path.splitext(files)[1]
            if "-" not in str(fileName):
                timeArray = time.localtime(int(fileName))
                otherStyleTime = time.strftime("%Y-%m-%d-%H-%M-%S", timeArray)
                '新的文件路径'
                newDirPath = os.path.join(path, environment + "-" + otherStyleTime + fileType)
                '重命名'
                os.rename(oldDirPath, newDirPath)
    except Exception as e:
        raise Exception("测试报告重命名失败,请检查！！！")

if __name__ == '__main__':
    rename(reports_path)
    # print(os.path.abspath(os.path.dirname(os.getcwd()))+"\\reports")
    # timeStamp = 1381419602
    # timeArray = time.localtime(timeStamp)
    # otherStyleTime = time.strftime("%Y-%m-%d-%H-%M-%S", timeArray)
    # print(otherStyleTime)
# print '***获取当前目录***'
# print os.getcwd()
# print os.path.abspath(os.path.dirname(__file__))
#
# print '***获取上级目录***'
# print os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
# print os.path.abspath(os.path.dirname(os.getcwd()))
# print os.path.abspath(os.path.join(os.getcwd(), ".."))
#
# print '***获取上上级目录***'
# print os.path.abspath(os.path.join(os.getcwd(), "../.."))

# 时间戳转换
# timeStamp = 1381419600
# timeArray = time.localtime(timeStamp)
# otherStyleTime = time.strftime("%Y-%m-%d-%H-%M-%S", timeArray)